##############################################################
#Figure 6: Hurricane Proximity $\uparrow$ Private Insurance Inquiries
##############################################################

df <- read.dta13("./temp/fig6data.dta")
head(df)

newdf <- df %>% 
  gather(variable, value, -var)%>%
  spread(var, value)

newdf$id <- c(3,5,1,4,2)


ggplot() + 
  geom_pointrange(data=newdf, mapping=aes(x=id, y=new2min_coef, ymin=new2min_ci_upper, 
                                          ymax=new2min_ci_lower),  color="goldenrod1", size=2, lwd=2, fatten=2) +
  coord_flip() + 
  theme_bw() + 
  ylim(-4,2) +
  scale_x_discrete(limit=c("1", "2", "3", "4", "5"), labels= c("Health", "Home", "Car", "Life", "Flood")) + 
  xlab("Insurance Type Searched")  + ylab("Estimate") +
  geom_hline(yintercept=0, col="grey60", lwd=1, lty="dashed", alpha=.4) + 
  theme(panel.grid.major = element_blank(),
        axis.text = element_text(size=12), 
        axis.title = element_text(size=15))

ggsave("./figures/fig6a.pdf")

data <- read.csv("./input/gtrends_monthly_regressiondata.csv")
data$dist.group<-data$distgroup
var <- c("flood", "car", "health", "home", "life", "dist.group")
data <- data[var]


df <- data %>% 
  gather(variable, value, -dist.group)

new2 <- group_by(df, variable, dist.group) %>% summarize(m = mean(value, na.rm=T))
new2$label2 <- new2$variable
new2$label2 <- factor(new2$label2,levels=c("flood","life","car", "home", "health"))
new2 <- na.omit(new2)

ggplot(new2, aes(x=as.factor(dist.group), y=m, group=variable)) +
  geom_line(size=0.9, alpha=0.2,
            data=new2[, c("dist.group", "m", "variable")], color="grey60") +
  geom_line(size=1.1, color="goldenrod1") + 
  ylab("Inquiry Intensity") + xlab("Distance Group") +
  theme_bw() +
  theme(panel.grid.minor = element_blank(),
        panel.grid.major = element_blank()) + 
  theme(strip.background=element_blank()) +
  facet_wrap(~ label2, nrow=5) + 
  scale_x_discrete(limit=c("1", "2", "3", "4", "5", "6", "7"), 
                   labels=c("(30,50]", "(50,100]", "(100,200]", "(200,500]",  "(500,1000]", 
                            "(1000,1500]", "(1500,2000]", ">2000")) + 
  geom_hline(yintercept=100, col="grey", lwd=.4, lty="dotted", alpha=.7) + 
  theme(axis.text.x = element_text(angle = 45, hjust = 1),
        axis.text = element_text(size=12), 
        axis.title = element_text(size=15))  


ggsave("./figures/fig6b.pdf")
